#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "user.h"

struct user * new_user(int id, 
			char *name, 
			int sex,
			int school, 
			int hp, 
			int hp_eff,
			int mp, 
			int strong, 
			int wisdom,
			int physique, 
			int agile, 
			int wen,
			char *skill,
			char *face,
			char *equip_a,
			char *equip_b,
			char *equip_c,
			char *equip_d,
			char *equip_e,
			char *equip_f,
			char *equip_g
			)
{
	struct user *user = (struct user*)malloc(sizeof(struct user));

	user->id 	= id;
	user->name	= new_string(name);
	user->sex	= sex;
	user->school	= school;
	user->hp	= hp;
	user->hp_eff	= hp_eff;
	user->mp	= mp;
	user->strong	= strong;
	user->wisdom	= wisdom;
	user->physique	= physique;
	user->agile	= agile,
	user->wen	= wen,
	user->skill	= new_string(skill);
	user->face	= new_string(face);
	user->equip_a	= new_string(equip_a);
	user->equip_b	= new_string(equip_b);
	user->equip_c	= new_string(equip_c);
	user->equip_d	= new_string(equip_d);
	user->equip_e	= new_string(equip_e);
	user->equip_f	= new_string(equip_f);
	user->equip_g	= new_string(equip_g);

	INIT_LIST_HEAD(&user->items);
	INIT_LIST_HEAD(&user->summons);

	return user;
}

struct user *get_user(int id)
{
	struct user *user;

	user = (struct user *)lru_cache_find(id);
	if (user == NULL)
	{
		user = db_load_user(id);
		if (user == NULL)
		{
	  		fprintf(stderr, "user dosen't exist"); 
			return NULL;
		}
		lru_cache_add(user);
	}

	return user;
}

int add_user_item(struct user *user, struct item *item)
{
	list_add_head(&item->list, &user->items);
	return 1;
}

int add_user_summon(struct user *user, struct summon *summon)
{
	list_add_head(&summon->list, &user->summons);
	return 1;
}
